package com.STS.ELearning.DAO
{
	
	import com.STS.ELearning.VO.KategorilerVO;
	import com.STS.ELearning.VO.KonularOnkonularVO;
	import com.STS.ELearning.VO.KonularVO;
	
	import flash.data.SQLConnection;
	import flash.data.SQLStatement;
	import flash.errors.SQLError;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.utils.StringUtil;
	
	
	public class Konular extends BaseDao
	{
	
		private static const TABLE_NAME:String="Konular";
		public function Konular( lock: SingletonLock) {
			super(TABLE_NAME);
		}
		private static var instance:Konular;
		public static function getInstance():Konular {
			if( instance == null )
				instance = new Konular( new SingletonLock );
			return instance;
		}
		public override function getTableContent(order:String=null,where:String=null,whereParams:Array=null):Array{
			var o:String="idKategori,sira";
			if(order!=null && StringUtil.trim(order).length>0)
			{
				o=order;
			}
			var data:Array=super.getTableContent(o,where,whereParams);
			var arrlist:Array=new Array();
			if(data!=null)
				for each(var item:Object in data)
			{
				arrlist.push( new KonularVO(item));
			}
			return arrlist;
		}
		
		public function updateRow( rowItem:Object):Boolean {
			var text:String = 'UPDATE main.'+TABLE_NAME+' SET  ad = @ad, aciklama = @aciklama, sira = @sira, otarih = @otarih, dtarih = @dtarih, video = @video, metin = @metin, idKategori = @idKategori, surec = @surec WHERE  main.'+TABLE_NAME+'.id = @id;';
			var params:Array = [ {name:"id", value:rowItem.id}, {name:"ad", value:rowItem.ad}, {name:"aciklama", value:rowItem.aciklama}, {name:"sira", value:rowItem.sira}, {name:"otarih", value:rowItem.otarih}, {name:"dtarih", value:rowItem.dtarih}, {name:"video", value:rowItem.video}, {name:"metin", value:rowItem.metin}, {name:"idKategori", value:rowItem.idKategori}, {name:"surec", value:rowItem.surec} ];
			return executeSingleResult(text,params);
		}
		
		public function insertRow( rowItem:Object):Boolean {
			var text:String= 'INSERT INTO  main.'+TABLE_NAME+'( id, ad, aciklama, sira, otarih, dtarih, video, metin, idKategori, surec ) VALUES ( '+getId()+',@ad,@aciklama,@sira,@otarih,@dtarih,@video,@metin,@idKategori,@surec );';
			var params:Array = [ {name:"ad", value:rowItem.ad}, {name:"aciklama", value:rowItem.aciklama}, {name:"sira", value:rowItem.sira}, {name:"otarih", value:rowItem.otarih}, {name:"dtarih", value:rowItem.dtarih}, {name:"video", value:rowItem.video}, {name:"metin", value:rowItem.metin}, {name:"idKategori", value:rowItem.idKategori}, {name:"surec", value:rowItem.surec} ];
			return executeSingleResult(text,params,rowItem,"id");
		}
		
		public function deleteRow( rowItem:Object ):Boolean {
			var text:String= 'DELETE FROM  main.'+TABLE_NAME+' WHERE  main.'+TABLE_NAME+'.id = @id;';
			var params:Array = [ {name:"id", value:rowItem.id} ];
			return executeSingleResult(text,params);
		}
		
		public function onKonuEkle(rowItem:KonularOnkonularVO):Boolean
		{
			var text:String= 'INSERT INTO  main.KonularOnkonular( idKonu, idOnkonu ) VALUES ( @idKonu,@idOnkonu );';
			var params:Array = [ {name:"idKonu", value:rowItem.idKonu}, {name:"idOnKonu", value:rowItem.idOnkonu} ];
			
			var stmt:SQLStatement=new SQLStatement();
			stmt.sqlConnection=super.sqlConnection;
			stmt.addEventListener(SQLErrorEvent.ERROR,sqlErrorHandler);
			stmt.text=text;
			super.setParameters(stmt,params);
			return super.getSingleResultBool(stmt);
		}
		public function OnKonulariSil(idKonu:int,idOnKonu:int=-1):Boolean
		{
			var text:String= 'Delete from  main.KonularOnkonular Where idKonu=@idKonu;';
			var params:Array = [ {name:"idKonu", value:idKonu} ];
			if(idOnKonu>0)
			{
				text+=" and idOnkonu=@idOnkonu";
				params.push({name:"idOnkonu",value:idOnKonu});
			}
			var stmt:SQLStatement=new SQLStatement();
			stmt.sqlConnection=super.sqlConnection;
			stmt.addEventListener(SQLErrorEvent.ERROR,sqlErrorHandler);
			stmt.text=text;
			super.setParameters(stmt,params);
			return super.getSingleResultBool(stmt);
		}
	
	}
}

class SingletonLock {}
